package cn.icatw.admin.controller;

import cn.icatw.admin.common.R;
import cn.icatw.admin.common.ResultStatusEnum;
import cn.icatw.admin.common.exception.CustomException;
import cn.icatw.admin.domain.Image;
import cn.icatw.admin.domain.Type;
import cn.icatw.admin.service.ImageService;
import cn.icatw.admin.service.TypeService;
import cn.icatw.admin.utils.ImageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * 壁纸表(Image)表控制层
 *
 * @author icatw
 * @since 2022-10-14 19:58:55
 */
@Api(tags = "壁纸表(Image)")
@RestController
@RequestMapping("image")
public class ImageController {

    /**
     * 服务对象
     */
    @Resource
    private ImageService imageService;
    @Resource
    private TypeService typeService;
    @Resource
    private ImageUtil imageUtil;

    /**
     * 分页查询所有数据
     */
    @ApiOperation(value = "分页查询所有数据 壁纸表")
    @GetMapping
    public R page(@RequestParam(required = false) String name, @RequestParam int current, @RequestParam int size) {
        Page<Image> page = new Page<>(current, size);
        QueryWrapper<Image> wrapper = null;
        if (!StringUtils.isEmpty(name)) {
            wrapper = new QueryWrapper<Image>().like("image_name", name);
        }
        Page<Image> imagePage = this.imageService.page(page, wrapper);
        List<Image> imageList = imagePage.getRecords();
        imageList.forEach(image -> {
            Long typeId = image.getTypeId();
            Type type = typeService.getById(typeId);
            image.setTypeName(type.getTypeName());
        });
        return R.ok(imagePage);
    }


    /**
     * 通过主键查询单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据 壁纸表")
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.imageService.getById(id));
    }

    /**
     * 新增数据
     */
    @ApiOperation(value = "新增数据 壁纸表")
    @PostMapping("save")
    public R save(@RequestBody Image image) {
        String imageUrl = image.getImageUrl();
        String imageSize = null;
        try {
            imageSize = imageUtil.getImageSize(imageUrl);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException(ResultStatusEnum.SYSTEM_EXCEPTION);
        }
        image.setImageSize(imageSize);
        image.setDownloads(0L);
        return R.ok(this.imageService.save(image));
    }

    /**
     * 修改数据
     */
    @ApiOperation(value = "修改数据 壁纸表")
    @PostMapping("update")
    public R updateById(@RequestBody Image image) {
        String imageUrl = image.getImageUrl();
        String imageSize = null;
        try {
            imageSize = imageUtil.getImageSize(imageUrl);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException(ResultStatusEnum.SYSTEM_EXCEPTION);
        }
        image.setImageSize(imageSize);
        return R.ok(this.imageService.updateById(image));
    }

    /**
     * 单条/批量删除数据
     */
    @ApiOperation(value = "单条/批量删除数据 壁纸表")
    @PostMapping("delete")
    public R delete(@RequestBody List<Long> ids) {
        return R.ok(this.imageService.removeByIds(ids));
    }
}

